(function($){
	$("button#exportar-cv", "#container-<?php echo $this->pre ?>").button({ icons:{primary:"ui-icon-document"} });
    $("button#renovar"    , "#container-<?php echo $this->pre ?>").button({ icons:{primary:"ui-icon-refresh"} });
    $("button#edit1"      , "#container-<?php echo $this->pre ?>").button({ icons:{primary:"ui-icon-pencil"} });
    $("button#change-img" , "#container-<?php echo $this->pre ?>").button({ icons:{primary:"ui-icon-image"} });

	jQuery.wijmo.wijexpander.animations.animate2 = function (options) {
        if (options.expand)
            options.content.show("puff", options, 300);
        else
            options.content.hide("explode", options, 300);
    }

    jQuery.wijmo.wijaccordion.animations.animate = function (options) {
        this.slide(options, {
            easing: options.down ? "easeOutBounce" : "swing",
            duration: options.down ? 1000 : 200
        });
    }

	$("div#accordion", "div#container-<?php echo $this->pre ?>").wijaccordion({
        requireOpenedPane: false,
        animated: "animate"
    });

    $("div#accordion", "#container-<?php echo $this->pre ?>").wijaccordion("activate", 1);
    $("div#accordion", "#container-<?php echo $this->pre ?>").wijaccordion("activate", 2);
    $("div#accordion", "#container-<?php echo $this->pre ?>").wijaccordion("activate", 3);
    $("div#accordion", "#container-<?php echo $this->pre ?>").wijaccordion("activate", 4);

	$( "#t_sector_emp" ).autocomplete({
		source: function(request, response) {
			$.ajax({ url: "<?php echo site_url('empresa/sectores_emp/autocomplete'); ?>",
				data:
				{
					term: $("#t_sector_emp").val()
				},
				dataType: "json",
				type: "POST",
				success: function(data){
					response(data);
				}
			});
		},
		focus: function( event, ui ){
			return false;
		},
		select: function( event, ui ){
			$( "#id_sector_emp").val( ui.item.id );
			$( "#t_sector_emp").val( ui.item.nombre );
			return false;
		},
		minLength: 0
	}).data( "autocomplete" )._renderItem = function( ul, item ) {
		return $( "<li></li>" )
		.data( "item.autocomplete", item )
		.append( "<a>" + item.nombre + "</a>" )
		.appendTo( ul );
	};

	$( "#t_poblacion" ).autocomplete({
		source: function(request, response) {
			$.ajax({ url: "<?php echo site_url('poblacion/autocompletefull'); ?>",
				data:
				{
					term     : $("#t_poblacion").val(),
					provincia: $("#t_provincia").val(),
					pais     : $("#t_pais").val()
				},
				dataType: "json",
				type: "POST",
				success: function(data){
					response(data);
				}
			});
		},
		focus: function( event, ui ){
			return false;
		},
		select: function( event, ui ){
			$( "#id_poblacion"  ).val( ui.item.id_poblacion    );
			$( "#t_poblacion"   ).val( ui.item.poblacion       );
			$( "#t_provincia"   ).val( ui.item.provincia       );
			$( "#t_pais"        ).val( ui.item.pais            );
			$( "#codigo_postal" ).val( ui.item.postal          );
			return false;
		},
		minLength: 2
	}).data( "autocomplete" )._renderItem = function( ul, item ) {
		return $( "<li></li>" )
		.data( "item.autocomplete", item )
		.append( "<a>"  + "<div style='width:250px;float:left;'>"+item.poblacion+"</div>" + " | " + item.provincia  + "</a>" )
		.appendTo( ul );
	};

	$( "#t_pais" ).autocomplete({
		source: function(request, response) {
			$.ajax({ url: "<?php echo site_url('pais/autocomplete'); ?>",
				data:
				{
					term  : $("#t_pais").val()
				},
				dataType: "json",
				type: "POST",
				success: function(data){
					response(data);
				}
			});
		},
		focus: function( event, ui ){
			return false;
		},
		select: function( event, ui ){
			$( "#t_pais"      ).val( ui.item.nombre          );
			return false;
		},
		minLength: 0
	}).data( "autocomplete" )._renderItem = function( ul, item ) {

		return $( "<li></li>" )
		.data( "item.autocomplete", item )
		.append( "<a>"  + item.nombre  + "</a>" )
		.appendTo( ul );
	};

	$( "#t_provincia" ).autocomplete({
		source: function(request, response) {
			$.ajax({ url: "<?php echo site_url('provincia/autocomplete'); ?>",
				data:
				{
					term  : $("#t_provincia").val()
				},
				dataType: "json",
				type: "POST",
				success: function(data){
					response(data);
				}
			});
		},
		focus: function( event, ui ){
			return false;
		},
		select: function( event, ui ){
			$( "#t_provincia"      ).val( ui.item.nombre          );
			$( "#t_pais"           ).val( ui.item.pais            );
			return false;
		},
		minLength: 0
	}).data( "autocomplete" )._renderItem = function( ul, item ) {

		return $( "<li></li>" )
		.data( "item.autocomplete", item )
		.append( "<a>"  + "<div style='width:250px;float:left;'>"+item.nombre+"</div>" + " | " + item.pais  + "</a>" )
		.appendTo( ul );
	};

	var vias=[];
	$.post("<?=site_url('via/autocomplete')?>",{ partida:"" },function(data){
		vias=jQuery.parseJSON(data);
		jQuery.each(vias, function(i, val) {
			val.label=val.nombre;
		});

		$("#via").autocomplete({
			source: vias,
			focus: function( event, ui ){
				return false;
			},
			select: function( event, ui ){
				$( "#via" ).val( ui.item.nombre);
				return false;
			},
			minLength: 0
		}).data( "autocomplete" )._renderItem = function( ul, item ) {
			return $( "<li></li>" )
			.data( "item.autocomplete", item )
			.append( "<a>" + item.nombre  + "</a>" )
			.appendTo( ul );
		};
	});

	
	function editar(selector){
		var buttonLabel = $(selector).button("option", "label");
        if(buttonLabel === "Editar"){

            $("a", ".close-tag").live("click", function(event){
                event.preventDefault();
            });

            $("a", ".title-table-with-tags").live("click", function(event){
                event.preventDefault();
            });

            $(".title-table-with-tags").each(function(){
                htmlTitleTag = $(selector).html() + "&nbsp&nbsp&nbsp&nbsp<a href='#' class='ui-state-highlight'>Agregar</a>";
                $(selector).html(htmlTitleTag);
            })

            $(selector).button("option", "label", "Guardar").button("option", "icons", {primary:'ui-icon-disk'});
            $("input,textarea", ".ui-widget-content", "#form-<?php echo $this->pre ?>").removeClass("ui-widget-content").addClass("form-elm-active").removeAttr("readonly");
            $('.table-with-tags tr').each(function() {
                $(selector).find("td").each(function() {
                    var htmlTD = $(selector).html() + "<a href='#' class='close-tag' ><span class='ui-icon ui-icon-close' style='float:right; margin-top:-3px;'></span></a>";
                    $(selector).html(htmlTD);
                    //alert(htmlTD);
                });
            });
        }else{
            $(selector).button("option", "label", "Editar").button("option", "icons", {primary:'ui-icon-pencil'});
            $(".form-elm-active").removeClass("form-elm-active").addClass("ui-widget-content").attr("readonly", "readonly");
            $('.table-with-tags tr').each(function() {
                $(selector).find("td").each(function() {
                    $(selector).html("Se asigna la que es!");
                    //alert(htmlTD);
                });
            });

            //enviar =$(this);

			formid  ='form-<?php echo $this->pre ?>';//enviar.offsetParent().attr("id");
			campos  =['id','cif','nif','nombre_com','razon_soc','id_sector_emp','telefono','movil','sms','contacto','cargo_con','email_con','id_poblacion','via','nombre_via','numero_via','direccion','fax','telefono2','web','codigo_postal','actividad','fecha_reg','id_agente','id_usuario'];
			data   =prepara_post('',campos,formid);
			resp   =hace_post('<?php echo site_url($this->url."guardar") ?>',data,false);
			muestra_msj(formid,campos,resp);
        }
	}
	//onclick en editar
	$("button#edit1", "#container-<?php echo $this->pre ?>").button({ icons:{primary:"ui-icon-pencil"} }).live("click", function(){
        selector=$(this).attr('id');
        editar('#'+selector);
    });

})(jQuery);
